package com.nxk.modules.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nxk.modules.core.dao.SysMenuRoleDao;
import com.nxk.modules.core.entity.SysMenuRole;
import com.nxk.modules.core.entity.vo.MenuRoleVo;
import com.nxk.modules.core.service.SysMenuRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuRoleServiceImpl extends ServiceImpl<SysMenuRoleDao, SysMenuRole> implements SysMenuRoleService {

    @Autowired
    private SysMenuRoleDao sysMenuRoleDao;

    /**
     * 保存给某个角色分配多个菜单权限
     * @param menuIds
     * @param roleId
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public boolean save(List<String> menuIds,List<String> falseList, String roleId) {
        //先删除此角色原来的权限数据
        QueryWrapper<SysMenuRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMenuRole::getRoleId,roleId);
        super.remove(queryWrapper);
        //添加新的权限数据
        List<SysMenuRole> list = new ArrayList<>();
        menuIds.stream().forEach(menuId ->{
            SysMenuRole sysMenuRole = new SysMenuRole();
            sysMenuRole.setMenuId(menuId);
            if(falseList.contains(menuId)){
                sysMenuRole.setIsSelect(SysMenuRole.SELECT_FALSE);
            }else {
                sysMenuRole.setIsSelect(SysMenuRole.SELECT_TRUE);
            }

            sysMenuRole.setRoleId(roleId);
            list.add(sysMenuRole);
        });
        boolean flag = super.saveBatch(list);
        return flag;
    }

    /**
     * 根据角色id查询菜单权限
     * @param roleId
     * @return
     */
    @Override
    public List<MenuRoleVo> queryMenuByRoleId(String roleId) {
        return sysMenuRoleDao.queryMenuByRoleId(roleId);
    }




    /**
     * 根据角色编码查询菜单权限
     * @param roleCode
     * @return
     */
    @Override
    public List<MenuRoleVo> queryMenuByRoleCode(String roleCode) {
        return sysMenuRoleDao.queryMenuByRoleCode(roleCode);
    }




}
